﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using mamnontuoitho7.AppCode;

namespace mamnontuoitho7.module
{
    public partial class ArticleDetails : System.Web.UI.Page
    {

        private mamnontuoitho7.AppCode.Article _article = new mamnontuoitho7.AppCode.Article();
        protected Product _product = new Product();
        protected General _general = new General();
        protected Comment _comment = new Comment();


        protected DataTable _dt, _dtLink, _dtComment;
        protected String _productID;// static vì muốn luu lại giá trị productID khi back lai trang

        protected int _pageflag;
        protected int _currentbar = 1;

        protected int _totalPages;
        protected int _totalCommentNumber;
        protected int _totalBars;

        protected int _beginPage;// dung bên trang giao diện
        protected int _endPage;// dung bên trang giao diện

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["memberID"].ToString() != "" && (Session["role"].ToString() == "1"))
            {
                try
                {
                    if (Request.QueryString["ArticleID"] != null)
                    {
                        _productID = Request.QueryString["ArticleID"].ToString();
                    }

                    _dt = _article.GetArticleByID(_productID);// lấy thông tin product(article) hien hành
                    // _dtComment = _comment.GetCommentByProductID(_productID);// lấy comment theo product Id hiện hành
                    _dtLink = _article.GetSomeTopArticleByType("6", "2");// lấy 6 product mói nhất -> link bài viết liên quan

                    //////////////////////////////////////////begin
                    _totalCommentNumber = _comment.TotalCommentOfOneProduct(_productID).GetValueOrDefault();
                    _totalPages = _general.Divide(_totalCommentNumber, ConstantClass._pageStepComment);
                    _totalBars = _general.Divide(_totalPages, ConstantClass._barStepComment);

                    if (Request.QueryString["page"] != null)// khi bấm vào các ô số
                    {

                        _pageflag = Convert.ToInt16(Request.QueryString["page"].ToString());// trang hiện hành

                        _dtComment = _comment.GetCommentPaged(_pageflag, ConstantClass._pageStepComment, _productID);
                    }
                    if (Request.QueryString["page"] == null && Request.QueryString["a"] == null)// khi mới vào trang
                    {
                        _currentbar = 1;
                        _pageflag = 1;
                        _dtComment = _comment.GetCommentPaged(1, ConstantClass._pageStepComment, _productID);
                    }

                    if (Request.QueryString["a"] != null && Request.QueryString["a"] == "1") // khi bấm vào ô >> (next)
                    {

                        _pageflag = _currentbar * ConstantClass._barStepComment + 1;
                        _dtComment = _comment.GetCommentPaged(_pageflag, ConstantClass._pageStepComment, _productID);
                        _currentbar += 1;
                    }

                    if (Request.QueryString["a"] != null && Request.QueryString["a"] == "-1") // khi bấm vào ô << (previous)
                    {
                        _currentbar -= 1;
                        _pageflag = (_currentbar - 1) * ConstantClass._barStepComment + 1;
                        _dtComment = _comment.GetCommentPaged(_pageflag, ConstantClass._pageStepComment, _productID);

                    }

                    _beginPage = (_currentbar - 1) * ConstantClass._barStepComment + 1;
                    _endPage = _currentbar * ConstantClass._barStepComment;

                    if (_endPage > _totalPages)
                    {
                        _endPage = _totalPages;
                    }
                    /////////////////////////////////////////end


                    if (Request.QueryString["ArticleID"] != null && Request.QueryString["Comment"] == null)// chỉ định duy nhât là refresh hay chạy lại link mới tang số view
                    {
                        if (!IsPostBack)
                        {
                            _product.IncreaseProductView(_productID);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorLoginPage);
                }

            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }

        protected void btnPostComment_Click(object sender, EventArgs e)
        {
            try
            {
                bool d = _comment.CommentCreate(_productID, Session["memberID"].ToString(), txtComment.Text.Trim());
                txtComment.Text = "";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorLoginPage);
            }
            Response.Redirect("ArticleDetails.aspx?ArticleID=" + _productID + "&Comment=1");// Comment=1 : tranh việc post cmt xong tăng số view

        }
    }
}